package leetcode.offer;

import leetcode.base.ListNode;

import java.util.List;

/**
 * ClassName: ReverseList
 * Description:
 * date: 2021-10-28 20:50
 *
 * @author liyifan
 */
public class ReverseList {
    public ListNode reverseList(ListNode head) {
        ListNode cur = head;
        ListNode pre = null;
        while (cur != null) {
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }

    public ListNode reverseList1(ListNode head) {
        return recur(head, null);
    }

    public ListNode recur(ListNode cur, ListNode pre){
        if (cur == null) {
            return pre;
        }
        ListNode res = recur(cur.next, cur);
        cur.next = pre;
        return res;
    }
}
